package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.DesignModuleDto;
import com.guigu.erp.mapper.DesignModuleMapper;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.DesignModule;
import com.guigu.erp.pojo.DesignModuleDetails;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.DesignModuleDetailsService;
import com.guigu.erp.service.DesignModuleService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.ModuleFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ClassName:DesignModuleServiceImpl
 * Package:com.guigu.erp.service.impl
 * Description:
 *
 * @Date:2024/2/28 20:53
 * @Author:chenchong
 */
@Service
public class DesignModuleServiceImpl extends ServiceImpl<DesignModuleMapper, DesignModule>
        implements DesignModuleService {
    /**
     * 获取Id最大物料设计单的设计单的编号
     * @return
     */
    @Autowired
    private DesignModuleMapper designModuleMapper;
    @Autowired
    private DesignFileService designFileService;
    @Autowired
    private DesignModuleDetailsService designModuleDetailsService;
    @Override
    public String getMaxDesignId() {
        DesignModule designModule = designModuleMapper.queryMaxModule();
        if(designModule==null){
            return null;
        }
        String designId = designModule.getDesignId();
        return designId;
    }

    @Transactional
    @Override
    public void makeDesignModule(DesignModuleDto designModuleDto) {
        //添加产品物料表
        this.save(designModuleDto);
        //添加产品物料明细表
        List<DesignModuleDetails> details = designModuleDto.getDetails();
        for(int i=0;i<details.size();i++){
            DesignModuleDetails designModuleDetails = details.get(i);
            //每条id明细设置为null
            designModuleDetails.setId(null);
            //获取物料设计单ID
            int id = designModuleDto.getId();
            //设置父级ID
            designModuleDetails.setParentId(id);
            //设置物料序号
            designModuleDetails.setDetailsNumber(i+1);
            //设置可用数量
            designModuleDetails.setResidualAmount(designModuleDetails.getAmount());
            // 插入物料明细
            designModuleDetailsService.save(designModuleDetails);

        }
        // 3. 修改产品档案的物料设计标志 W001-1: 已设计
        String productId =designModuleDto.getProductId();
        QueryWrapper<DesignFile> qw=new QueryWrapper<DesignFile>();
        qw.eq("product_id",productId);
        DesignFile designFile =designFileService.getOne(qw);
        designFile.setDesignModuleTag("W001-1");
        designFileService.updateById(designFile);
    }
    /**
     * 查询未审核的产品物料表所有信息
     */
    @Override
    public PageUtils<DesignModule> show() {

        QueryWrapper<DesignModule> qw=new   QueryWrapper<DesignModule>();
        qw.eq("CHECK_TAG","S001-0");

        List<DesignModule> designModules = designModuleMapper.selectList(qw);

        PageInfo<DesignModule> pageInfo = new PageInfo<>(designModules);
        PageUtils<DesignModule> p=new PageUtils<DesignModule>();
        p.setRows(pageInfo.getList());
        p.setTotal(pageInfo.getTotal());
        return p;
    }

    @Override
    public PageUtils<DesignModule> moduleShow(ModuleFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignModule> qw = new QueryWrapper<DesignModule>();
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                qw.eq("FIRST_KIND_ID", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                qw.eq("SECOND_KIND_ID", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                qw.eq("THIRD_KIND_ID", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_productName())) {
                qw.eq("PRODUCT_NAME", vo.getSearch_productName());
            }
        if (!StringUtils.isEmpty(vo.getSearch_checkTag())) {
                qw.eq("CHECK_TAG", vo.getSearch_checkTag());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                qw.ge("REGISTER_TIME", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                qw.le("REGISTER_TIME", vo.getSearch_registerTime2());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<DesignModule> list = designModuleMapper.selectList(qw);
        PageInfo<DesignModule> pageInfo = new PageInfo<>(list);
        // PageInfo 转换成 PageUtils
        PageUtils<DesignModule> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;

    }
    /**
     * 查询所有物料不重复的名字
     */
    @Override
    public List<DesignFile> commodity() {
//        String hao="Y001-1";
        List<DesignFile> commodity = designModuleMapper.commodity();
        return commodity;
    }
}
